package vn.edu.saigontech.ncms.web.teacher;

import java.util.Collection;
import java.util.Date;

import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateTeacherAction extends AbstractValidator {
	
	private String id;
	private String firstName;
	private String lastName;
	private String dateOfBirth;
	private String gender;
	private String email;

	public String check() {
		if(Checker.isEmpty(this.firstName)) {
			this.sendKeyError("validator.required", "firstName");
		}
		if(Checker.isEmpty(this.lastName)) {
			this.sendKeyError("validator.required", "lastName");
		}
		if(!Checker.isEmpty(this.dateOfBirth) && !Checker.isDate(this.dateOfBirth, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "dateOfBirth", "format.date");
		}
		if(Checker.isEmpty(this.gender)) {
			this.sendKeyError("validator.required", "gender"); 
		}
		if(!Checker.isEmpty(this.email)) {
			if(!Checker.isEmail(this.email)) {
				this.sendKeyError("validator.invalid", "email");
			}
		}
		if(Checker.isEmpty(this.id)) {
			Collection<Teacher> teachers = TeacherService.INSTANCE.findAll();
			for(Teacher teacher : teachers) {
				if(teacher.isSameName(firstName, lastName)) {
					if (!Checker.isEmpty(this.dateOfBirth)) {
						Date dob = DateUtil.fromString(dateOfBirth, this.i18n("format.date"));
						if(teacher.isDupplicate(firstName, lastName, dob)) {
							this.sendKeyError("validator.dupplicate", "firstName", "lastName", dateOfBirth);
						}
					}
					else {
						this.sendKeyError("validator.sameName", "firstName", "lastName");
					}
				}
			}
		}
		return this.toSuccess();
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
}